package kadledav.adventure.framework.model.world.impl;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import kadledav.adventure.framework.model.general.ActionObject;
import kadledav.adventure.framework.model.world.Item;
import kadledav.adventure.framework.model.world.Location;
import kadledav.adventure.framework.model.world.Passage;
import kadledav.adventure.framework.model.world.Player;

/**
 * This player cannot recognize a victory
 * 
 * @author David
 * 
 */
public class PlayerBasic implements Player {

	protected Location location;
	protected List<Item> inventory;

	public PlayerBasic(Location startLocation) {
		this(startLocation, null);
	}

	public PlayerBasic(Location startLocation, List<Item> startInventory) {
		location = startLocation;
		if (startInventory == null) {
			startInventory = new LinkedList<Item>();
		}
		inventory = startInventory;
	}

	@Override
	public boolean pickUpItem(Item item) {
		if (item == null) {
			return false;
		}
		if (location.playerTookItem(item)) {
			if (inventory.add(item)) {
				return true;
			} else {
				location.playerDropItem(item);
			}
		}
		return false;
	}

	@Override
	public boolean dropItem(Item item) {
		if (item == null) {
			return false;
		}
		if (inventory.remove(item)) {
			if (location.playerDropItem(item)) {
				return true;
			} else {
				inventory.add(item);
			}
		}
		return false;
	}

	@Override
	public boolean goThroughPassage(Passage passage) {
		if (passage == null || passage.getPointedLocation() == null) {
			return false;
		}
		location = passage.getPointedLocation();
		return true;
	}

	@Override
	public String doAction(ActionObject actionObject) {
		if (!canBeObjectUsed(actionObject)) 
			return cannotUseObject(actionObject);
		String result = actionObject.doAction(inventory);
		return (result != null && !result.isEmpty()) ? result : "Nevim co s tim mam delat.";

	}

	@Override
	public String doActionWith(ActionObject actionObject1, ActionObject actionObject2) {
		if (!canBeObjectUsed(actionObject1))
			return cannotUseObject(actionObject1);
		if (!canBeObjectUsed(actionObject2))
			return cannotUseObject(actionObject2);
		String result1 = actionObject1.doActionWith(actionObject2, inventory);
		String result2 = actionObject2.doActionWith(actionObject1, inventory);
		String resultComplete = (result1 == null ? "" : result1) + (result2 == null ? "" : result2);
		return resultComplete.trim().isEmpty() ? "Tato kombinace nema smysl." : resultComplete;
	}

	private String cannotUseObject(ActionObject actionObject2) {
		return "Objekt " + actionObject2.getName() + " nemohu pouzit. Mozna bych ho mohl zkusit dat do inventare...";
	}

	private boolean canBeObjectUsed(ActionObject actionObject) {
		if (actionObject instanceof Item) {
			Item item = (Item) actionObject;
			if (!inventory.contains(item) && item.isTakeable()) {
				return false;
			}
		}
		return true;
	}

	@Override
	public Location getCurrentLocation() {
		return location;
	}

	@Override
	public List<Item> getInventoryItems() {
		return Collections.unmodifiableList(inventory);
	}
}
